var api = require('../../config/api.js');
var util = require('../../utils/util.js');
var user = require('../../utils/user.js');

var app = getApp();
// pages/login/login.js
Page({
data:{
  encryptedData:'',
  iv:''
},

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    // 页面渲染完成

  },
  onReady: function () {

  },
  onShow: function () {
    // 页面显示
  },
  onHide: function () {
    // 页面隐藏

  },
  onUnload: function () {
    // 页面关闭

  },
  disLogin(){
    console.log('aaa')
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  wxLogin: function (e) {
    console.log(e.detail.userInfo)
    wx.setStorageSync('userInfo', e.detail.userInfo);
    let that=this
    wx.getUserInfo({
      success: function (res) {
        // console.log(res)
      that.setData({
        encryptedData: res.encryptedData,
        iv:res.iv
      })
        if (e.detail.userInfo == undefined) {
          app.globalData.hasLogin = false;
          util.showErrorToast('微信登录失败1');
          return;
        }
        // console.log("11111" + that.data.encryptedData)
        user.checkLogin().catch(() => {

          user.loginByWeixin(that.data.encryptedData, that.data.iv, e.detail.userInfo).then(res => {
            app.globalData.hasLogin = true;
            console.log("success")
            wx.navigateBack({
              delta: 1
            })
          }).catch((err) => {
            app.globalData.hasLogin = false;
            util.showErrorToast('微信登录失败2');
          });

        });
      }
    })

   
  },
  accountLogin: function () {
    wx.navigateTo({
      url: "/pages/accountLogin/accountLogin"
    });
  }
})